<?php
namespace app\common\model;

use think\Model;

class Coupon extends Model
{
    protected $table = "zj_coupon";
    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = 'delete_time';
    protected $coupon ;

	// 验证规则
	protected $rule = [
		//['id|优惠券id', ['require']],
	]; 
	
	// 验证场景
	protected $scene = [
        //'update' => ['id'],
    ];

    // 验证器
	public function intValidate(){
        $validate = new \think\Validate($this->rule);
        foreach ($this->scene as $k1=>$v1)
            $validate->scene($k1, $v1);
        return $validate;
    }

    /**
     * 卡券编号生成规则
     */
    private function _createCouponNo()
    {
        $chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
        $str = '';
        for ($i = 0; $i < 8; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return date('YmdHis', time()) . $str;
    }

    /**
     * 添加卡券
     */
    public function insertCoupon($data = array()){
	    $cou = new Coupon();
        $data['coupon_no'] = $cou->_createCouponNo();
        $cou->data($data)->save();
        return $cou->id;
    }

    /**
     * 更新卡券
     */
    public function updateCoupon($data = array()){
        $cou = new Coupon();
        $res = $cou->save($data,['id'=>$data['id']]);
        return $res;
    }

    /**
     * 获取卡券列表
    */
    public function getCouponList(){
        $cou = new Coupon();
        $data = $cou->paginate(20);
        return $data;
    }

    /**
     * 获取店铺卡券列表
     */
    public function getStoreCouponList($store_id,$pagesize){
        $keyword = input('param.keyword');
        $where = [];
        if($keyword)
        {
            $where['coupon_name'] = ['like', "%$keyword%"];
        }
        $where['status'] = ['lt',3];
        $cou = new Coupon();
        $data = $cou->where('create_id','eq',$store_id)->where($where)->paginate($pagesize, false, [
            'query' => request()->param(),
        ]);
        if(!empty($data)){
            foreach($data as $k => $v){
                if($v['activity_end_time'] < time() && $v['status'] == 1){
                    $cou->where('id','eq',$v['id'])->update(['status'=>2]);
                }
            }
        };
        return $data;
    }

    /**
     * 获取卡券用户领取列表
     */
    public function getUserCouponList($coupon_id){
        $usercoupnlist = Db("zj_coupon_use")->alias("cu")
            ->join("user u","u.user_id = cu.user_id")
            ->field("cu.*,u.nickname as user_name,u.user_phone")
            ->where("cu.coupon_id = $coupon_id")
            ->select();
        return $usercoupnlist;
    }

    /**
     * 获取卡券信息
     */
    public function getCouponInfo($id){
        $data = Db("zj_coupon")->where("id = $id")->find();
        return $data;
    }

    /**
     * 获取店铺列表
     */
    public function getStoreList(){
        $storelist = Db("store")
            ->field("store_merchant_id as store_id,store_name")
            ->where("store_verify_status = 2 and store_is_forever_close = 0 and store_is_close = 0 and store_is_delete = 0 ")
            ->select();
        return $storelist;
    }

    /**
     * 获取信息
*/
    public function getStoreinfo($merchant_id){
        $storelist = Db("store")
            ->field("store_id,store_name")
            ->where("store_verify_status = 2 and store_is_forever_close = 0 and store_is_close = 0 and store_is_delete = 0 ")
            ->where("store_merchant_id = $merchant_id")
            ->find();
        return $storelist;
    }

    /**
     * 删除卡券
     */
    public function delCoupon($id){
        $cou = new Coupon();
        $data = [
            'status'=>3,
            'delete_time'=>time(),
        ];
        $res = $cou->where(['id'=>$id])->update($data);
        return $res;
    }

    /**
     * 禁用用户卡券
     */
    public function delUserCoupon($id){
        $cou_user = Db("zj_coupon_use");
        $data = [
            'status'=>3,
            'delete_time'=>time(),
        ];
        $res = $cou_user->where("id = $id")->update($data);
        return $res;
    }

}

  





 
